package com.quanquan.store.message;

import com.quanquan.store.entity.marketActivity.scratchCard.ScratchCard;
import com.quanquan.store.entity.marketActivity.scratchCard.ScratchCardChance;
import com.quanquan.store.service.marketActivity.ScratchCardService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import utils.DateUtils;

import java.io.IOException;
import java.util.Date;
import java.util.Objects;


/**
 * 功能描述:给用户添加优惠券次数的队列
 *
 * @ClassName: MqSendCouponReceiver
 * @Author: xiaoqianh丶
 * @Date: 2019-04-05 23:02
 * @Version: V1.0
 */
@Slf4j
@Component
public class MqScratchReceiver {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ScratchCardService scratchCardService;

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "addTimes",
                    durable = "true"),
            exchange = @Exchange(value = "scratch.exchange",
                    type = "topic"),
            ignoreDeclarationExceptions = "true",
            key = "scratch.#")})
    @RabbitHandler
    public void checkSignContinuous(Message message, Channel channel) throws IOException {

        String userId = Objects.requireNonNull(message.getHeaders().get("userId")).toString();
        String idInfo = Objects.requireNonNull(message.getHeaders().get("scratchId")).toString();
        Long id = Long.parseLong(idInfo);
        System.out.println("id:........"+ id);
        System.out.println("userId:........"+ userId);
        //手工ack
        long deliveryTag = Long.parseLong(Objects.requireNonNull(message.getHeaders().get(AmqpHeaders.DELIVERY_TAG)).toString());
        channel.basicAck(deliveryTag, false);



        ScratchCard scratchCard = scratchCardService.getById(id);


        if(DateUtils.compare_date(scratchCard.getStartTime(),new Date()) > 0){
            return ;
        }
        if(DateUtils.compare_date(scratchCard.getEndTime(),new Date()) <= 0){
            return ;
        }

        ScratchCardChance scratchCardChance = scratchCardService.getByIdAndUserId(scratchCard.getId(), userId);
        if(null != scratchCardChance){
            scratchCardChance.setChance(scratchCardChance.getChance() + 1);
            scratchCardChance.setUpdateTime(new Date());
        }else{
            scratchCardChance = new ScratchCardChance();
            scratchCardChance.setUserId(userId);
            scratchCardChance.setScratchCardId(id);
            scratchCardChance.setChance(1);
            scratchCardChance.setCreateTime(new Date());
            scratchCardChance.setUpdateTime(new Date());
        }

        scratchCardService.saveOrUpdate(scratchCardChance);

    }

}